package cn.tedu.demo.controller;

import cn.hutool.json.JSON;
import cn.tedu.demo.common.Result;
import cn.tedu.demo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/files")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public Result<?> upload(MultipartFile file) throws IOException {
        String path = fileService.upload(file);
        return Result.success(path);
    }

    @PostMapping("/editor/upload")
    public JSON editorUpload(MultipartFile file) throws IOException {
        JSON json = fileService.editorUpload(file);
        return json;
    }

    @GetMapping("/{flag}")
    public void getFiles(@PathVariable String flag, HttpServletResponse response) {
        fileService.getFiles(flag,response);
    }

}
